package com.zte.market.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.market.dao.CustomDao;
import com.zte.market.model.Custom;
import com.zte.market.model.CustomCondition;
import com.zte.market.service.CustomService;
import com.zte.sys.login.model.LUsers;
import com.zte.sys.user.model.Users;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class CustomServiceImpl extends ServiceImpl<CustomDao,Custom> implements CustomService {
    @Autowired
    private CustomDao customDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public PageModel<Custom> pageCustomInfoByCondition(PageModel<Custom> pageModel, CustomCondition condition) {
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        PageInfo<Custom> pageInfo=new PageInfo<>(customDao.queryCustomByCondition(condition));
        return PageModel.getPageModel(pageModel,pageInfo);
    }

    @Override
    public PageModel<Custom> pageCustomInfoByConditionAndUid(PageModel<Custom> pageModel, CustomCondition condition) {
        //当用户权限未总经理时可查询全部，所以将userid查询条件去除
        LUsers lUsers=(LUsers) redisTemplate.opsForValue().get(String.valueOf(condition.getUserId()));
        if(lUsers.getJobId()==1) {
            condition.setUserId(null);
        }
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        PageInfo<Custom> pageInfo=new PageInfo<>(customDao.queryCustomByconditionAndUid(condition));
        return PageModel.getPageModel(pageModel,pageInfo);
    }

    @Override
    public Custom getCustomById(Integer customId) {
        CustomCondition condition=new CustomCondition();
        condition.setCustomId(customId);
        return customDao.queryCustomByCondition(condition).get(0);
    }
}
